/**
 * 生成任务详情相关页面饼图
 */
export default (data, filter) => {
  let selectLegend = {};
  let disableNum = 0;
  if (filter) {
    disableNum = 0;
    data.forEach((el) => {
      if (!el.value) {
        disableNum += 1;
        selectLegend[el.name] = false;
      } else {
        selectLegend[el.name] = true;
      }
    });
  }
  if (data.length === disableNum) {
    return null;
  }
  let option = {
    tooltip: {
      formatter: '{b}: {d}%',
    },
    legend: {
      itemWidth: 14,
      itemHeight: 14,
      bottom: 0,
      left: 20,
      borderRadius: 0,
      itemGap: 20,
      textStyle: {
        fontSize: 13,
      },
      selected: selectLegend,
    },
    series: {
      type: 'pie',
      center: ['50%', '35%'],
      radius: [0, '50%'],
      itemStyle: Object.values(selectLegend).filter(r => {return r}).length > 1 ? {
        normal: {
          borderWidth: 3,
          borderColor: '#ffffff',
        },
      } : {
        normal: {
          borderWidth: 0,
          borderColor: '#ffffff',
        },
      },
      data: data.map((item) => {
        if (Array.isArray(item.color)) {
          item.itemStyle = {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y: 1,
              colorStops: [
                {
                  offset: 0,
                  color: item.color[0],
                },
                {
                  offset: 1,
                  color: item.color[1],
                },
              ],
            },
          };
        } else {
          item.itemStyle = {
            color: item.color,
          };
        }
        item.labelLine = {
          lineStyle: {
            color: '#000000',
          },
        };
        item.label = {
          formatter: '{b}: {d}%',
        };
        return item;
      }),
    },
  };
  return option;
};
